package com.itly.controller;

import com.itly.entity.Answer;
import com.itly.entity.FillQuestion;
import com.itly.entity.JudgeQuestion;
import com.itly.entity.MultiQuestion;
import com.itly.service.FillQuestionService;
import com.itly.service.JudgeQuestionService;
import com.itly.service.MultiQuestionService;
import com.itly.util.ApiResult;
import com.itly.service.AnswerService;
import com.itly.util.ApiResultHandler;
import com.itly.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class AnswerController {
    private Logger logger = LoggerFactory.getLogger(AnswerController.class);

    @Autowired
    private AnswerService answerService;

    @Autowired
    private MultiQuestionService multiQuestionService;

    @Autowired
    private FillQuestionService fillQuestionService;

    @Autowired
    private JudgeQuestionService judgeQuestionService;

    @GetMapping("/answers/{page}/{size}")
    public ApiResult answers(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageBean<Answer> answerPageInfo = answerService.selectAnswerByPage(page, size);
        return ApiResultHandler.buildApiResult(200, "所有数据", answerPageInfo);
    }

    @GetMapping("question/{questionId}/{type}")
    public ApiResult selectQuestion(@PathVariable("questionId") Integer questionId, @PathVariable("type") String type) {
        return answerService.selectQuestion(questionId, type);
    }

    @PutMapping("questionMulti")
    public ApiResult updateQuestionMulti(@RequestBody MultiQuestion multiQuestion) {
        int i = multiQuestionService.updateQuestion(multiQuestion);
        if (i == 1) {
            logger.info("更新选择题成功");
            return ApiResultHandler.buildApiResult(200, "更新选择题成功", i);
        } else {
            logger.info("更新选择题失败",multiQuestion);
            return ApiResultHandler.buildApiResult(400, "更新选择题失败", i);
        }
    }

    @PutMapping("questionFill")
    public ApiResult updateQuestionFill(@RequestBody FillQuestion fillQuestion) {
        int i = fillQuestionService.updateQuestion(fillQuestion);
        if (i == 1) {
            logger.info("更新填空题成功");
            return ApiResultHandler.buildApiResult(200, "更新填空题成功", i);
        } else {
            logger.info("更新填空题失败",fillQuestion);
            return ApiResultHandler.buildApiResult(400, "更新填空题失败", i);
        }
    }

    @PutMapping("questionJudge")
    public ApiResult updateQuestionJudge(@RequestBody JudgeQuestion judgeQuestion) {
        int i = judgeQuestionService.updateQuestion(judgeQuestion);
        if (i == 1) {
            logger.info("更新判断题成功");
            return ApiResultHandler.buildApiResult(200, "更新判断题成功", i);
        } else {
            logger.info("更新判断题失败",judgeQuestion);
            return ApiResultHandler.buildApiResult(400, "更新判断题失败", i);
        }
    }

    @DeleteMapping("question/{questionId}/{type}")
    public ApiResult deleteQuestion(@PathVariable("questionId") Integer questionId, @PathVariable("type") String type) {
        return answerService.deleteQuestion(questionId, type);
    }
}
